import type {CreateProductRequest, Product, UpdateProductRequest} from '@/types/business/product';
import type {PageResponse} from '@/types/common/base';
import {ApiClient} from './api';
import {createMessageConfig} from '@/utils/responseHandler';

export default {
    // 获取所有产品（分页）
    getProducts(page = 0, size = 20, sortBy = 'id', sortDir = 'asc') {
        return ApiClient.get<PageResponse<Product>>('/products', {
            params: {page, size, sortBy, sortDir},
            showSuccess: false
        });
    },

    // 获取所有产品（不分页）
    getAllProducts() {
        return ApiClient.get<Product[]>('/products/all', {showSuccess: false});
    },

    // 根据ID获取产品
    getProductById(id: number) {
        return ApiClient.get<Product>(`/products/${id}`, {showSuccess: false});
    },

    // 根据产品代号获取产品
    getProductByCode(productCode: string) {
        return ApiClient.get<Product>(`/products/code/${productCode}`, {showSuccess: false});
    },

    // 检查产品代号是否存在
    checkProductCodeExists(productCode: string) {
        return ApiClient.get<boolean>(`/products/check-code/${productCode}`, {showSuccess: false});
    },

    // 创建产品
    createProduct(product: CreateProductRequest) {
        return ApiClient.post<Product>('/products', product, createMessageConfig('产品创建成功'));
    },

    // 更新产品
    updateProduct(id: number, product: UpdateProductRequest) {
        return ApiClient.put<Product>(`/products/${id}`, product, createMessageConfig('产品更新成功'));
    },

    // 删除产品
    deleteProduct(id: number) {
        return ApiClient.delete<void>(`/products/${id}`, createMessageConfig('产品删除成功'));
    },

    // 高级搜索产品
    searchProducts(searchRequest: any) {
        return ApiClient.post<PageResponse<Product>>('/products/search', searchRequest, {showSuccess: false});
    }
};